package com.pur.zsms.unusual.dao.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Query;

import net.sf.json.JSONObject;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import com.pur.zsms.common.dao.GenericDao;
import com.pur.zsms.common.dao.JpaTemplate;
import com.pur.zsms.common.exception.GenericException;
import com.pur.zsms.common.support.PageController;
import com.pur.zsms.common.support.PaginationSupport;
import com.pur.zsms.common.util.CipherUtil;
import com.pur.zsms.foodmanage.food.po.FoodMaterPo;
import com.pur.zsms.foodmanage.food.po.FoodPo;
import com.pur.zsms.matermanage.mater.dao.IMaterDao;
import com.pur.zsms.matermanage.mater.po.MaterLevelPo;
import com.pur.zsms.matermanage.mater.po.MaterPo;
import com.pur.zsms.matermanage.mater.vo.MaterVo;
import com.pur.zsms.ordercustommanage.dao.ISelectFoodDao;
import com.pur.zsms.ordercustommanage.po.OrderCustFoodPo;
import com.pur.zsms.ordercustommanage.po.OrderCustPo;
import com.pur.zsms.ordercustommanage.po.OrderSupplierMaterNeedPo;
import com.pur.zsms.ordercustommanage.po.OrderSupplierMaterPo;
import com.pur.zsms.ordercustommanage.po.OrderSupplierMaterRealPo;
import com.pur.zsms.ordercustommanage.po.OrderSupplierPo;
import com.pur.zsms.ordercustommanage.vo.OrderCustVo;
import com.pur.zsms.ordercustommanage.vo.OrderStateVo;
import com.pur.zsms.pmsnmanage.menu.po.MenuPo;
import com.pur.zsms.pmsnmanage.user.po.UserPo;
import com.pur.zsms.pmsnmanage.user.po.UserRolePo;
import com.pur.zsms.pmsnmanage.user.vo.UserVo;
import com.pur.zsms.supplier.po.ShopSupplierTypePo;
import com.pur.zsms.systemsetting.datadictionary.po.CodeType;
import com.pur.zsms.unusual.dao.IUnusualDao;
import com.pur.zsms.unusual.po.OrderSupplierMaterUnusualPo;
import com.pur.zsms.unusual.vo.OrderSupplierMaterUnusualVo;

@Repository("unusualDao")
public class UnusualDaoImpl extends GenericDao implements IUnusualDao {
	public void addOrderSupplierMaterUnusualPo(OrderSupplierMaterUnusualPo c) throws GenericException {
		this.merge(c);
	}

	public PaginationSupport findUnusualOrder(PageController pageController,
	        OrderSupplierMaterUnusualVo tOrderSupplierMaterUnusualVo) throws GenericException {

		StringBuffer ql = new StringBuffer();
		ql.append("SELECT a.oid,a.ord_id, a.ord_cust_id, b.real_name, a.ord_sup_id, c.sup_companyname,a.ord_date, a.ord_payment_state,a.ord_state FROM t_order_supplier a, t_user b, t_supplier c WHERE a.ord_cust_id = b.user_id AND a.ord_sup_id = c.sup_id AND EXISTS (SELECT 1 FROM t_order_supplier_mater_unusual d WHERE d.ord_id = a.ord_id AND d.ord_sup_id=a.ord_sup_id) ");
		if (tOrderSupplierMaterUnusualVo.getOrdCustId() != 0) {
			ql.append(" AND a.ord_cust_id = '" + tOrderSupplierMaterUnusualVo.getOrdCustId() + "'");
		}

		return this.findByNativeQuery(ql.toString(), pageController);
	}

	public PaginationSupport findUnusualDetailed(PageController pageController,
	        OrderSupplierMaterUnusualVo tOrderSupplierMaterUnusualVo, String oid, String supId)
	        throws GenericException {

		StringBuffer ql = new StringBuffer();
		ql.append("SELECT a.oid, a.ord_id, a.ord_cust_id, b.real_name, a.ord_sup_id, c.sup_companyname, a.ord_date, a.ord_payment_state, a.ord_state , d.ord_mater_id, e.mater_name, e.unit, e.brands, e.specifications, d.ord_mater_weight as realweight, d.unusual_deal, d.unusual_deal_flag, d.unusual_remark,f.`ord_mater_weight`  as weight,d.unusual_deal_detailed,d.self_ord_id  FROM t_order_supplier a, t_user b, t_supplier c , t_order_supplier_mater_unusual d, t_material e,t_order_supplier_mater f WHERE a.ord_cust_id = b.user_id AND a.ord_sup_id = c.sup_id AND d.ord_id=a.ord_id AND d.ord_sup_id= a.ord_sup_id AND e.mater_id = d.ord_mater_id  AND f.ord_id = d.ord_id  AND f.ord_sup_id = d.ord_sup_id AND f.`ord_mater_id` = d.`ord_mater_id`  AND a.ord_id ='"
		        + oid + "' AND a.ord_sup_id='" + supId + "' ");
		return this.findByNativeQuery(ql.toString(), pageController);
	}

	@Override
	public List<OrderSupplierMaterUnusualPo> getOrderSupplierMaterUnusualPoList(int ordId, int supId,
	        int ordMaterId) throws GenericException {
		JpaTemplate template = this.getTemplate();
		OrderCustPo c = new OrderCustPo();
		StringBuffer ql = new StringBuffer();
		ql.append(" select o ");
		ql.append(" from OrderSupplierMaterUnusualPo o where o.ordId='" + ordId + "' AND o.ordMaterId = '"
		        + ordMaterId + "' AND o.ordSupId = '" + supId + "'");
		List<OrderSupplierMaterUnusualPo> list = template.find(ql.toString(), null);
		return list;
	}

	public List<Object[]> judgeAllUnusual(int ordId, int supId) throws GenericException {

		StringBuffer ql = new StringBuffer();
		ql.append("  SELECT 1 FROM t_order_supplier_mater_unusual a WHERE a.ord_id= '" + ordId
		        + "' AND a.ord_sup_id='" + supId + "' AND a.unusual_deal_flag=0 ");
		List list = this.entityManager.createNativeQuery(ql.toString()).getResultList();
		return list;
	}

}
